package api

import (
	"cgs-server/helper"
	"cgs-server/server"
	"cgs-server/server/assets/cgs_portal"
	"cgs-server/server/base"
	"cgs-server/server/cgs/app/models"
	"cgs-server/server/cgs/app/service"
	"cgs-server/server/cgs/app/store"
	"go.mongodb.org/mongo-driver/bson"
	"net/http"
)

type MarketHandler struct {
	// srv service
	srv service.Service
	// store
	store store.Factory
}

func NewMarketHandler(store store.Factory) *MarketHandler {
	return &MarketHandler{
		srv:   service.NewService(store),
		store: store,
	}
}

func (wg *MarketHandler) Get(w http.ResponseWriter, r *http.Request) {
	id := r.FormValue("id")
	if id == "" {
		_, _ = helper.WriteJSON(w, server.Fail("group id is required"))
		return
	}
	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	group, err := wg.srv.Market().Detail(id, user)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK().WithData(group))
}

func (wg *MarketHandler) Delete(w http.ResponseWriter, r *http.Request) {
	id := r.FormValue("id")
	if id == "" {
		_, _ = helper.WriteJSON(w, server.Fail("group id is required"))
		return
	}
	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	err = wg.srv.Market().Delete(id, user)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK())
}

func (wg *MarketHandler) Save(w http.ResponseWriter, r *http.Request) {
	var market models.Market
	err := helper.ReadRequestBody(&market, w, r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}

	if market.VisualType != "" && !cgs_portal.ValidVisualType(market.VisualType) {
		_, _ = helper.WriteJSON(w, server.Fail("invalid visualType"))
		return
	}

	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	err = wg.srv.Market().Save(&market, user)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK().WithData(bson.M{"id": market.GetId()}))
}

func (wg *MarketHandler) Update(w http.ResponseWriter, r *http.Request) {
	var wsGroup models.Market
	err := helper.ReadRequestBody(&wsGroup, w, r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	err = wg.srv.Market().Update(&wsGroup, user)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK())
}

func (wg *MarketHandler) List(w http.ResponseWriter, r *http.Request) {
	user, err := base.GetCurrentUser(r)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	list, err := wg.srv.Market().List(user)
	if err != nil {
		_, _ = helper.WriteJSON(w, server.Fail(err.Error()))
		return
	}
	_, _ = helper.WriteJSON(w, server.OK().WithData(list))
}
